package com.jxl.domain.handler.subject;

import com.jxl.common.enums.SubjectTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题目类型工厂
 * InitializingBean接口是bean进行初始化的时候提供一个方法进行操作的一个接口
 */
@Component
public class SubjectTypeHandlerFactory  implements InitializingBean {

    /**
     * 这种方式就是将所有SubjectTypeHandler类型的Handler装配进来
     */
    @Resource
    private List<SubjectTypeHandler> subjectTypeHandlers;

    /**
     * 提供一个map，将枚举和hanlder进行匹配
     */
    private Map<SubjectTypeEnum,SubjectTypeHandler> handlerMap = new HashMap<>();


    /**
     * 提供一个handler
     * @param subjectType
     * @return
     */
    public SubjectTypeHandler getHandler(int subjectType){
        SubjectTypeEnum subjectTypeEnum = SubjectTypeEnum.getCode(subjectType);
        return handlerMap.get(subjectTypeEnum);
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        for (SubjectTypeHandler subjectTypeHandler:subjectTypeHandlers) {
            handlerMap.put(subjectTypeHandler.getHandlerType(),subjectTypeHandler);
        }
    }
}
